//
//  WNOperateView.m
//  Whatnet_iOS
//
//  Created by Bill on 2019/5/25.
//  Copyright © 2019 什么网络. All rights reserved.
//

#import "WNOperateView.h"

@interface WNOperateView ()

@property (nonatomic, strong) UIButton *backBtn;
@end

@implementation WNOperateView

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
        self.frame = WNApp.delegate.window.bounds;
        self.backBtn.frame = self.bounds;
        self.contentView.frame = self.bounds;
        [self setShowType:WNOperateViewShowTypePresent];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]){
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
        self.frame = WNApp.delegate.window.bounds;
        self.backBtn.frame = self.bounds;
        self.contentView.frame = self.bounds;
        [self setShowType:WNOperateViewShowTypePresent];
    }
    return self;
}

- (void)setShowType:(WNOperateViewShowType)showType
{
    _showType = showType;
    switch (showType) {
        case WNOperateViewShowTypeAlert:{
        }break;
        case WNOperateViewShowTypePresent:{
        }break;
        default:break;
    }
}

- (void)show
{
    [WNApp.delegate.window addSubview:self];
    [self layoutIfNeeded];
    switch (self.showType) {
        case WNOperateViewShowTypeAlert:{
            self.contentView.alpha = 0.1;
            [UIView animateWithDuration:0.2 animations:^{
                self.contentView.alpha = 1;
            }];
        }break;
        case WNOperateViewShowTypePresent:{
            self.contentView.y = self.height;
            [UIView animateWithDuration:0.2 animations:^{
                self.contentView.y = self.height - self.contentView.height;
            }];
        }break;
        default:break;
    }
}
- (void)dismiss
{
    
    UITextField *tf = [self.window performSelector:@selector(firstResponder)];
    if (tf) {
        [tf resignFirstResponder];
        return;
    }
    
    switch (self.showType) {
        case WNOperateViewShowTypeAlert:{
            [UIView animateWithDuration:0.3 animations:^{
                self.alpha = 0;
            } completion:^(BOOL finished) {
                [self removeFromSuperview];
            }];
        }break;
        case WNOperateViewShowTypePresent:{
            [UIView animateWithDuration:0.3 animations:^{
                self.contentView.y = self.height;
            } completion:^(BOOL finished) {
                [self removeFromSuperview];
            }];
        }break;
        default:break;
    }
}

- (UIView *)contentView
{
    if (_contentView == nil){
        _contentView = UIView.new;
        [_contentView setBackgroundColor:[UIColor clearColor]];
        [self addSubview:_contentView];
    }
    return _contentView;
}

- (UIButton *)backBtn{
    if (!_backBtn) {
        _backBtn = [[UIButton alloc] init];
        [self addSubview:_backBtn];
        [_backBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchDown];
        _backBtn.backgroundColor = [UIColor clearColor];
    }
    return _backBtn;
}

- (void)backAction{
    [self dismiss];
}

- (void)dealloc
{
    NSLog(@"%@---dealloc", self.class);
}
@end
